<?php
class ProductsController extends AppController {

	var $name = 'Products';
	var $helpers = array('Html', 'Form', 'Javascript', 'TvFck');
	var $uses=array('Product','Shop','City','Setting','Guest','Order','Purchasing_document','Purchasing_document_item','Sale_document','Sale_document_item','Branch','Return_sale_document','Return_sale_document_item','Return_purchasing_document','Return_purchasing_document_item','Stockinventoryplus_document','Stockinventoryplus_item','Stockinventoryminus_document','Stockinventoryminus_item');


 function buy() {
		  $this->account();
		 // $conditions=array('News.status'=>1);
		$this->paginate = array('limit' => '20','order' => 'Purchasing_document.id DESC');
		$this->set('Purchasing_document', $this->paginate('Purchasing_document',array()));
		 
		  $category_code=$this->Session->read('category_code');
		  
		  $this->paginate = array('conditions'=>array('Product.status'=>1,'Product.category_code'=>$category_code),'limit' => '15','order' => 'Product.id DESC');
	      $this->set('product', $this->paginate('Product',array()));
        
	}
   
   /* TON KHO*/
	function stocks() {
		$this->account();
		
		/*Danh sach kho hang*/
		$companyname=$this->Session->read('companyname');
		
		$list_cat=$this->Branch->find('list',array('fields'=>array('Branch.code','Branch.name'),'conditions'=>array('Branch.companyname'=>$companyname)));
		//$this->Set('branch',$this->Branch->find('all'));
		
		$this->set(compact('list_cat'));
		
		 $category_code=$this->Session->read('category_code');
		
		   $category=array();
			foreach($category_code as $key=>$value){
				$category[$key]=trim($value);
			}
		
		
		
		$this->paginate = array('conditions'=>array('Product.companyname'=>$companyname,'or'=>array('Product.category_code'=>$category)),'limit' => '15','order' => 'Product.id DESC');
		
		
		$this->set('product', $this->paginate('Product',array()));
		
		
	}
	
	
	
	function search_kho()

	{ $this->account();
		$branch_code=$this->data['Product']['list_cat'];
		$companyname=$this->Session->read('companyname');
		
		$a=$this->Purchasing_document->find('all',array('conditions'=>array('Purchasing_document.branch_code'=>$branch_code)));
		//pr($a); die;
		$product=array(); $n=0;
		foreach($a as $row){
			
			$b=$this->Purchasing_document_item->find('all',array('conditions'=>array('Purchasing_document_item.purchasing_documents_uuid'=>$row['Purchasing_document']['uuid'])));
			
			
			foreach($b as $b) 
			{
			
				$pr=$this->Product->find('list',array('fields'=>array('Product.category_code'),'conditions'=>array('Product.product_code'=>$b['Purchasing_document_item']['product_code'],'Product.companyname'=>$companyname)));
				
				foreach($pr as $key=>$value){
				//echo "aa"; die;
				$product[$n]['product_code']=$b['Purchasing_document_item']['product_code'];
				$product[$n]['soluongban']=0;
				$product[$n]['soluongbantralai']=0;
				$product[$n]['soluongmuatralai']=0;
				$product[$n]['dieuchinhthua']=0;
				$product[$n]['dieuchinhthieu']=0;
				$product[$n]['category_code']=$value;
				$product[$n++]['soluong']=$b['Purchasing_document_item']['total_unit'];
				}
				
			}
			//pr($product); die;
			
			$a=$this->Sale_document->find('all',array('conditions'=>array('Sale_document.branch_code'=>$branch_code)));
			foreach($a as $row){
				
				$b=$this->Sale_document_item->find('all',array('conditions'=>array('Sale_document_item.sale_documents_uuid'=>$row['Sale_document']['uuid'])));
				
				
				foreach($b as $b) 
				{
					foreach($product as $key=>$product1) {
					if($product1['product_code']==$b['Sale_document_item']['product_code'])
					{
						$product[$key]['soluongban']=$b['Sale_document_item']['total_unit'];
					
					}
					}
					
				}
				
			}
			
			/*Ban tra lai*/
			$a=$this->Return_sale_document->find('all',array('conditions'=>array('Return_sale_document.branch_code'=>$branch_code)));
			//pr($a); die;
			foreach($a as $row){
				
				$b=$this->Return_sale_document_item->find('all',array('conditions'=>array('Return_sale_document_item.return_sale_documents_uuid'=>$row['Return_sale_document']['uuid'])));
				
				//pr($b); die;
				foreach($b as $b) 
				{
					foreach($product as $key=>$product1) {
					if($product1['product_code']==$b['Return_sale_document_item']['product_code'])
					{
						$product[$key]['soluongbantralai']=$b['Return_sale_document_item']['total_unit'];
					
					}
					}
					
				}
				
			}
			
			/*End ban tra lai*/
			
			
			/*Mua tra lai*/
			$a=$this->Return_purchasing_document->find('all',array('conditions'=>array('Return_purchasing_document.branch_code'=>$branch_code)));
			//pr($a); die;
			foreach($a as $row){
				
				$b=$this->Return_purchasing_document_item->find('all',array('conditions'=>array('Return_purchasing_document_item.return_purchasing_documents_uuid'=>$row['Return_purchasing_document']['uuid'])));
				
				//pr($b); die;
				foreach($b as $b) 
				{
					foreach($product as $key=>$product1) {
					if($product1['product_code']==$b['Return_purchasing_document_item']['product_code'])
					{
						$product[$key]['soluongmuatralai']=$b['Return_purchasing_document_item']['total_unit'];
					
					}
					}
					
				}
				
			}
			
			/*End Mua tra lai*/
		
		
		/*Điều chỉnh thừa*/
			$a=$this->Stockinventoryplus_document->find('all',array('conditions'=>array('Stockinventoryplus_document.branch_code'=>$branch_code)));
			//pr($a); die;
			foreach($a as $row){
				
				$b=$this->Stockinventoryplus_item->find('all',array('conditions'=>array('Stockinventoryplus_item.stockinventoryplus_uuid'=>$row['Stockinventoryplus_document']['uuid'])));
				
				//pr($b); die;
				foreach($b as $b) 
				{
					foreach($product as $key=>$product1) {
					if($product1['product_code']==$b['Stockinventoryplus_item']['product_code'])
					{
						$product[$key]['dieuchinhthua']=$b['Stockinventoryplus_item']['total_unit'];
					
					}
					}
					
				}
				
			}
			
			/*End điều chỉnh thừa*/
			
			//pr($product); die;
			
			/*Điều chỉnh thiếu*/
			$a=$this->Stockinventoryminus_document->find('all',array('conditions'=>array('Stockinventoryminus_document.branch_code'=>$branch_code)));
			//pr($a); die;
			foreach($a as $row){
				
				$b=$this->Stockinventoryminus_item->find('all',array('conditions'=>array('Stockinventoryminus_item.stockinventoryminus_uuid'=>$row['Stockinventoryminus_document']['uuid'])));
				
				//pr($b); die;
				foreach($b as $b) 
				{
					foreach($product as $key=>$product1) {
					if($product1['product_code']==$b['Stockinventoryminus_item']['product_code'])
					{
						$product[$key]['dieuchinhthieu']=$b['Stockinventoryminus_item']['total_unit'];
					
					}
					}
					
				}
				
			}
			
			/*End điều chỉnh thiếu*/
			
			//pr($product); die;
		}
		//pr($product); die;
	
		
		
		
		$list_cat=$this->Branch->find('list',array('fields'=>array('Branch.code','Branch.name'),'conditions'=>array('Branch.companyname'=>$companyname)));
		$this->set(compact('list_cat'));
		$this->Session->write('product_ton1',$product);	
		
	
		
		$this->set('product',$product);
		$branch=$this->Branch->find('list',array('fields'=>array('Branch.name'),'conditions'=>array('Branch.code'=>$branch_code,'Branch.companyname'=>$companyname)));
		foreach($branch as $key=>$value) {$name=$value;}
		
		$this->set('product',$product);
		$this->set('branch_code',$name);
		$this->Session->write('branch_code',$name);
	}		
   
	function sales() {
		  $this->account();
		 // $conditions=array('News.status'=>1);
		  $this->paginate = array('conditions'=>array('Product.status'=>1),'limit' => '15','order' => 'Product.id DESC');
	      $this->set('product', $this->paginate('Product',array()));
		  $this->render('buy');
        
	}
	
	 function export_xls() {
		$this->Lading->recursive = 1;
		mysql_query("SET NAMES 'utf8'"); 
   		mysql_query("SET character_set_client=utf8"); 
  		mysql_query("SET character_set_connection=utf8");
	  $category_code=$this->Session->read('category_code');
		$this->paginate = array('conditions'=>array('or'=>array('Product.category_code'=>$category_code)),'limit' => '15','order' => 'Product.id DESC');
		
		
		$this->set('rows',$data);
		$this->render('export_xls','export_xls');
	}
	
	function export_txt() {
		$this->Lading->recursive = 1;
		mysql_query("SET NAMES 'utf8'"); 
   		mysql_query("SET character_set_client=utf8"); 
  		mysql_query("SET character_set_connection=utf8");
		
		$category_code=$this->Session->read('category_code');
		$this->paginate = array('conditions'=>array('or'=>array('Product.category_code'=>$category_code)),'limit' => '15','order' => 'Product.id DESC');
		
		
		$this->set('rows',$data);
		$this->render('export_txt','export_txt');
	}
	
	/*Update so luong*/
	function updateshopingcart1($id=null,$soluong=null, $str=null)
		{
	
		$chuoi=explode('|',$str);
		$chuoi1=$chuoi[0].'|'.$chuoi[1].'|'.$soluong;
		
			$a=$this->Order->findById($id);
			//pr($a); die;
			//echo $id.'--'.$str.'-----'.$chuoi1.'-----'.$a['Order']['product'];
			$a['Order']['product']=str_replace($str,$chuoi1,$a['Order']['product']);
			//echo $a['Order']['product']; die;
			$this->Order->save($a);
			
		$this->redirect(array('controller'=>'orders','action' => 'view/'.$id));
		}
	




	function index() {
		  $this->account();
		 // $conditions=array('News.status'=>1);
         $id=$this->Session->read('shop_id');
		   $category_code=$this->Session->read('category_code');
		   $companyname=$this->Session->read('companyname');
		   //echo $companyname;
		   $category=array();
			foreach($category_code as $key=>$value){
				$category[$key]=trim($value);
			}
			
		  
			
		  $this->paginate = array('conditions'=>array('or'=>array('Product.category_code'=>$category)),'limit' => '15','order' => 'Product.id DESC');
	      $this->set('product', $this->paginate('Product',array()));
		  $this->loadModel("Catproduct");
        $list_cat = $this->Catproduct->generatetreelist(null,null,null," _ ");
        $this->set(compact('list_cat'));
        
		
		$a=$this->Product->find('list',array('fields'=>array('Product.modified'),'conditions'=>array('Product.companyname'=>$companyname),'order'=>'Product.id DESC','limit'));
		
		$this->set('prod',$a);
    
        
	}
	
	
	function date($date)
	{
		$ngay = explode("-", $date);
		$date1=$ngay['2'].'-'.$ngay['1'].'-'.$ngay['0'];
		return  $date1;
	}
	
	//Them bai viet
	function add() {
		$this->account();
		$this->set('city',$this->City->find('all'));
		if (!empty($this->data)) {
			$this->Product->create();
			$data['Product'] = $this->data['Product'];
			$data['Product']['images']=$_POST['userfile1'];
			 
			$data['Product']['shop_id']=$this->Session->read('shop_id');
			
			$category_code=$this->Session->read('category_code');
			$data['Product']['category_code']=$category_code;
			
			if($_POST['date_batdau']!='')
			$data['Product']['date_batdau']=$this->date($_POST['date_batdau']);
			if($_POST['date_ketthuc']!='')
			$data['Product']['date_ketthuc']=$this->date($_POST['date_ketthuc']);
		
			$data['Product']['conlai']=$data['Product']['soluong']-$data['Product']['daban'];
			
			$data['Product']['price']=$this->chuantien($this->data['Product']['price']);
			
			$data['Product']['price_old']=$this->chuantien($this->data['Product']['price_old']);
			
			$data['Product']['giam']=round(($data['Product']['price_old']-$data['Product']['price'])*100/$data['Product']['price_old']);
		
			
		
// 			echo '<pre>';
// 			print_r($data);die();
			
			//$data['Product']['images_eg']=$_POST['userfile_eg'];
		//	$data['Product']['display']=$_POST['display'];
			if ($this->Product->save($data['Product'])) {
				$this->Session->setFlash(__('Thêm mới danh mục thành công', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thêm mơi danh mục thất bại. Vui lòng thử lại', true));
			}
		}
		$this->loadModel("Catproduct");
        $list_cat = $this->Catproduct->generatetreelist(null,null,null," _ ");
        $this->set(compact('list_cat'));
        
        
        $_list_part=$this->City->find('list',array('fields' => array('id', 'name')));
        $this->set('list_cat1',$_list_part);
        $this->set(compact('list_cat1'));
     
//         $list_cat1 = $this->City->find('all');
//         $this->set(compact('list_cat1'));
        
        
     
	}
	//view mot tin 
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('views', $this->Product->read(null, $id));
	}
	//dong danh muc
	function close($id=null) {
		$this->account();
		if (empty($id)) {
			$this->Session->setFlash(__('Khôn tồn tại danh mục này', true));
			$this->redirect(array('action'=>'index'));
		}
		$data['Product'] = $this->data['Product'];
		$data['Product']['id']=$id;
		$data['Product']['status']=0;		
		if ($this->Product->save($data['Product'])) {
			$this->Session->setFlash(__('Danh mục không được hiển thị', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Danh mục không close được', true));
		$this->redirect(array('action' => 'index'));

	}
	// kich hoat
	function active($id=null) {
		$this->account();
		if (empty($id)) {
			$this->Session->setFlash(__('Khôn tồn tại danh mục này', true));
			$this->redirect(array('action'=>'index'));
		}
		$data['Product'] = $this->data['Product'];
		$data['Product']['id']=$id;
		$data['Product']['status']=1;
		if ($this->Product->save($data['Product'])) {
			$this->Session->setFlash(__('Danh mục kích hoạt thành công', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Danh mục không kich hoạt được', true));
		$this->redirect(array('action' => 'index'));

	}
	// tim kiem san pham
	/*function search() {
		$data['Product']=$this->data['Product'];
		$category=$data['Product']['catproduct_id'];
		$this->paginate = array('conditions'=>array('Product.catproduct_id'=>$category),'limit' => '15','order' => 'Product.id DESC');
	    $this->set('product', $this->paginate('Product',array()));
		 $this->loadModel("Catproduct");
        $list_cat = $this->Catproduct->generatetreelist(null,null,null," _ ");
        $this->set(compact('list_cat'));
		
	}*/
	function search() {
		$this->loadModel("Catproduct");
	   $keyword="";
	   $list_cat="";
	  
	   if(isset($_POST['keyword']))
		$x['Product.title']=$_POST['keyword'];
		
		  $id=$this->Session->read('shop_id');
		   $category_code=$this->Session->read('category_code');
		   $companyname=$this->Session->read('companyname');
		   //echo $companyname;
		   $category=array();
			foreach($category_code as $key=>$value){
				$category[$key]=trim($value);
			}
			
		
		$this->paginate = array('conditions'=>array('or'=>array('Product.category_code'=>$category),$x),'limit' => '12','order' => 'Product.id DESC');
		$this->set('product', $this->paginate('Product',array()));	
		//$ketquatimkiem=$this->Product->find('all',array('conditions'=>$x,'order' => 'Product.id DESC','limit'=>3));	
		//pr($ketquatimkiem); die;
		//$this->set('products',$category);
		 $this->loadModel("Catproduct");
        $list_cat1 = $this->Catproduct->generatetreelist(null,null,null," _ ");
        $this->set(compact('list_cat'));
		
	}
	
	
	
	
	// sua tin da dang
	function edit($id = null) {
		$this->account();
	
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Không tồn tại ', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$data['Product'] = $this->data['Product'];
			
	
			$data['Product']['images']=$_POST['userfile1'];
			$data['Product']['conlai']=$data['Product']['soluong']-$data['Product']['daban'];
			
			if($data['Product']['date_batdau']!='') $data['Product']['date_batdau']=$this->date($_POST['date_batdau']);
		if($data['Product']['date_ketthuc']!='')$data['Product']['date_ketthuc']=$this->date($_POST['date_ketthuc']);

			 $this->chuantien($this->data['Product']['price']);
			
			$data['Product']['price']=$this->chuantien($this->data['Product']['price']);
			
			$data['Product']['price_old']=$this->chuantien($this->data['Product']['price_old']);
	
			$data['Product']['giam']=round(($data['Product']['price_old']-$data['Product']['price'])*100/$data['Product']['price_old']);
		
		
			//$data['Product']['display']=$_POST['display'];
			if ($this->Product->save($data['Product'])) {
				$this->Session->setFlash(__('Bài viết sửa thành công', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Bài viết này không sửa được vui lòng thử lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Product->read(null, $id);
		}
		$this->loadModel("Catproduct");
        $list_cat = $this->Catproduct->generatetreelist(null,null,null," _ ");
        $this->set(compact('list_cat'));
		$this->set('edit',$this->Product->findById($id));
		
		$_list_part=$this->City->find('list',array('fields' => array('id', 'name')));
        $this->set('list_cat1',$_list_part);
        $this->set(compact('list_cat1'));
		
		
	}
	function processing() {
		$this->account();
		if(isset($_POST['dropdown']))
			$select=$_POST['dropdown'];
			
		if(isset($_POST['checkall']))
				{
			
			switch ($select){
				case 'active':
				$products=($this->Product->find('all'));
				foreach($products as $product) {
					$product['Product']['status']=1;
					$this->Product->save($product['Product']);					
				}
				//vong lap active
				break;
				case 'notactive':	
				//vong lap huy
				$products=($this->Product->find('all'));
				foreach($products as $product) {
					$product['Product']['status']=0;
					$this->Product->save($product['Product']);					
				}
				break;
				case 'delete':
				$products=($this->Product->find('all'));
				foreach($products as $product) {
					$this->Product->delete($product['Product']['id']);					
				}
				if($this->Product->find('count')<1)
					$this->redirect(array('action' => 'index'));	
				 else
				 {
					$this->Session->setFlash(__('Danh mục không close được', true));
					$this->redirect(array('action' => 'index'));
				 }
				//vong lap xoa
				break;
				
			}
		}
		else{
			
			switch ($select){
				case 'active':
				$products=($this->Product->find('all'));
				foreach($products as $product) {
					if(isset($_POST[$product['Product']['id']]))
					{
						$product['Product']['status']=1;
						$this->Product->save($product['Product']);
					}
				}
				//vong lap active
				break;
				case 'notactive':	
				//vong lap huy
				$products=($this->Product->find('all'));
				foreach($products as $product) {
					if(isset($_POST[$product['Product']['id']]))
					{
						$product['Product']['status']=0;
						$this->Product->save($product['Product']);
					}
				}
				break;
				case 'delete':
				$products=($this->Product->find('all'));
				foreach($products as $product) {
					if(isset($_POST[$product['Product']['id']]))
					{
					    $this->Product->delete($product['Product']['id']);
						
					}
										
				}
				$this->redirect(array('action'=>'index'));
				die;	
				//vong lap xoa
				break;
				
			}
			
		}
		$this->redirect(array('action' => 'index'));
		
	}
	// Xoa cac dang
	function delete($id = null) {
		$this->account();		
		if (empty($id)) {
			$this->Session->setFlash(__('Khôn tồn tại bài viết này', true));
			//$this->redirect(array('action'=>'index'));
		}
		if ($this->Product->delete($id)) {
			$this->Session->setFlash(__('Xóa bài viết thành công', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Bài viết không xóa được', true));
		$this->redirect(array('action' => 'index'));
	}
	function _find_list() {
		return $this->Catproduct->generatetreelist(null, null, null, '__');
	}
	//check ton tai tai khoan
	function account(){
		if(!$this->Session->read("shop_id") || !$this->Session->read("name")){
			$this->redirect('/');
		}
	}
	// chon layout
	function beforeFilter(){
		$this->layout='admin';
	}

	function dinhdangngay($date=null){
		$date=explode("-", $date);
		$date1=$date['2'].'-'.$date['1'].'-'.$date['0'];
		return $date1;
		
	}
	
	function chuantien($tien){
	$a='/[^0-9]/';
	$b='';
	return (int)preg_replace($a,$b,$tien);
	
	}
	
		function get_product_by_code($id=null)	
	{
	//echo "a"; die;
		return $this->Product->findByProduct_code($id);
	}
	
	function ton_xls()
	{
			$this->Lading->recursive = 1;
			mysql_query("SET NAMES 'utf8'"); 
			mysql_query("SET character_set_client=utf8"); 
			mysql_query("SET character_set_connection=utf8");
			//echo $this->Session->read('shop_id');
			$data = $this->Session->read('product_ton1');
			//echo "nam"; 
				//pr($this->Session->read('product_ton1')); die;
			$name='TONKHO_'.$this->Session->read('branch_code');
			//pr($data); die;
			$this->set('branch_code',$this->Session->read('branch_code'));
			$this->set('name',$name);
			$this->set('product',$data);
			$this->render('ton_xls','export_xls');

	}	
	
	function ton_txt()
	{
			$this->Lading->recursive = 1;
			mysql_query("SET NAMES 'utf8'"); 
			mysql_query("SET character_set_client=utf8"); 
			mysql_query("SET character_set_connection=utf8");
			
			$data = $this->Session->read('product_ton');
			$name='TONKHO_'.$this->Session->read('branch_code');
			//pr($data); die;
		
			$this->set('branch_code',$this->Session->read('branch_code'));
			$this->set('name',$name);
			$this->set('product',$data);
			$this->render('ton_txt','export_txt');

	}	
	
	
	
	
	
	
}
?>
